﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SaaS
{
    public class MiniSaaSDB
    {
        private System.Data.SqlClient.SqlConnection con;
        System.Data.SqlClient.SqlDataReader oldReader;
        public MiniSaaSDB()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "workstation id=MiniSaaS.mssql.somee.com;packet size=4096;user id=SaaS_cse564;pwd=123abccse;data source=MiniSaaS.mssql.somee.com;persist security info=False;initial catalog=MiniSaaS";
        }
        ~MiniSaaSDB()
        {
            close();
        }
        public bool open()
        {
            con.Open();
            return (isOpen());
        }
        public bool close()
        {
            if (isOpen()){
                try
                {
                    con.Close();
                }
                catch (Exception e)
                {
                    return false;
                }
            }
            return true;
        }
        public bool isOpen()
        {
            return (con.State == System.Data.ConnectionState.Open);
        }
        public int executeNonQuery(String query)
        {
            if (!isOpen())
                return 0;
            if (oldReader != null && !oldReader.IsClosed)
                oldReader.Close();
            System.Data.SqlClient.SqlCommand cmd;
            cmd = new System.Data.SqlClient.SqlCommand(query, con);
            return cmd.ExecuteNonQuery();
        }
        public System.Data.SqlClient.SqlDataReader executeQuery(String query)
        {
            if (oldReader != null && !oldReader.IsClosed)
                oldReader.Close();
            try
            {
                System.Data.SqlClient.SqlCommand cmd;
                cmd = new System.Data.SqlClient.SqlCommand(query, con);
                oldReader = cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                return null;
            }
            return oldReader;
        }
    }
}